<?php
namespace App\Http\Controllers\AdminApi;
use App\Model\AdminTable;
use App\Model\ClassTable;
use App\Repository\BaseController\ApiController;
use App\Repository\Common\ConstTable;
use App\Repository\Sys\JsonRespose;
use App\Repository\Sys\Str;
//管理员管理
class AdminController extends ApiController{
	//管理员-列表
    public function getAdminList(){
        $page = \Request::get('page',1);
        $limit = \Request::get('page_num',10);
        $title = \Request::get('title');
        return JsonRespose::done(AdminTable::getAdminList($page,$limit,$title));
    }
    //管理员-添加/编辑
    public function postAdminSave(){
        //获取所有参数到$param数组
        $param = \Request::all();
        $status = \Request::get('status', 1);
        $data = [
            'id' => $param['id'],
            'id_join' => $param['id_join'],
            'phone' => $param['phone'],
            'name' => $param['name'],
            'pwd' => $param['pwd'],
            'status' => $status,
        ];
        //判断是否存在id
        if (isset($param['id']) and $param['id']) {
            //根据id更新数据
            $res = AdminTable::ORM()->where('id', $param['id'])->update($data);
        } else {
            //否则插入数据
            $res = AdminTable::ORM()->insertGetId($data);
            //自增id更新为排序id
            AdminTable::ORM()->where('id', $res)->update(['id' => $res]); //排序id
            $param['id'] = $res;
        }
        if ($res) {
            //接口返回数据
            return JsonRespose::success(['id' => $param['id']]);
        } else {
            return JsonRespose::fail();
        }
    }
    //管理员-详情
    public function getAdminInfo(){
        $id = \Request::get('id');
        return JsonRespose::done(AdminTable::getAdminInfo($id));
    }
    //管理员-状态修改
    public function postAdminStatus(){
        $id = \Request::get('id');
        $status = \Request::get('status');
        //取得当前数据
        $res = AdminTable::ORM()->where('id', $id)->update(['status' => $status]);
        if ($res) {
            return JsonRespose::success();
        } else {
            return JsonRespose::fail();
        }
    }
    //管理员-职位权限详情
    public function getAdminRootInfo(){
        $id = \Request::get('id');
        return JsonRespose::done(ClassTable::getClassInfo($id));
    }
    //管理员-职位权限保存
    public function postAdminRootSave(){
        //获取所有参数到$param数组
        $param = \Request::all();
        $root = '';
        //的判断是否存在 并且 是否是数组
        if (isset($param['root']) and  is_array($param['root'])) {
            $root = implode(',', $param['root']);  //使用逗号分隔字符
        }
        $data = [
            'id' => $param['id'],
            'root' => $root,
        ];
        //判断是否存在id
        if (isset($param['id']) and $param['id']) {
            //根据id更新数据
            $res = ClassTable::ORM()->where('id', $param['id'])->update($data);
        }
        if ($res) {
            //接口返回数据
            return JsonRespose::success(['id' => $param['id']]);
        } else {
            return JsonRespose::fail();
        }
    }
}